package com.kintreda.ieco_server.util.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.kintreda.ieco_server.util.DateUtil;
import com.kintreda.ieco_server.util.Parameters;
import com.kintreda.ieco_server.util.StringUtil;


/**
 * 文件上传工具类
 * @author HuangZhongYao
 *
 */
public class FileUtil {

	
	/****************************************************
	 * 文件上传
	 * @param file		文件信息
	 * c02：				参数配置中的文件保存根目录 d://web/download
	 * @param prefix	保存的相对路径,如 user,user/images...
	 * @return
	 */
	public static String upload(MultipartFile file, String prefix) {
		
		if (file != null && file.getSize() > 0) {
			String originalFilename = file.getOriginalFilename();

			//文件上传路径
			String basePath = Parameters.uploadBasePath;
			String newsPath = "/" + prefix + "/" + DateUtil.getDate(DateUtil.YYYYMM) + "/";
			String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

			String fileName = StringUtil.createId() + suffix;

			InputStream in = null;
			FileOutputStream out = null;
			File targetPath = new File(basePath + newsPath);
			if (!targetPath.exists()) {
				targetPath.mkdirs();
			}
			try {
				in = file.getInputStream();

				out = new FileOutputStream(targetPath.getAbsolutePath() + File.separator + fileName);

				byte[] buffer = new byte[4096];
				int read = 0;
				while ((read = in.read(buffer)) != -1) {
					out.write(buffer, 0, read);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
					}
					if (in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			return  Parameters.downloadBasePath + newsPath + fileName;
		}
		
		return "";
	}
	
	

	/****************************************************
	 *	 文件上传
	 *	返回值不带文件基本访问路径
	 * @param file		文件信息
	 * c02：				参数配置中的文件保存根目录 d://web/download
	 * @param prefix	保存的相对路径,如 user,user/images...
	 * @return 
	 */
	public static String uploadBasePath(MultipartFile file, String prefix) {
		
		if (file != null && file.getSize() > 0) {
			String originalFilename = file.getOriginalFilename();

			//文件上传路径
			String basePath = Parameters.uploadBasePath;
			String newsPath = "/" + prefix + "/" + DateUtil.getDate(DateUtil.YYYYMM) + "/";
			String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

			String fileName = StringUtil.createId() + suffix;

			InputStream in = null;
			FileOutputStream out = null;
			File targetPath = new File(basePath + newsPath);
			if (!targetPath.exists()) {
				targetPath.mkdirs();
			}
			try {
				in = file.getInputStream();

				out = new FileOutputStream(targetPath.getAbsolutePath() + File.separator + fileName);

				byte[] buffer = new byte[4096];
				int read = 0;
				while ((read = in.read(buffer)) != -1) {
					out.write(buffer, 0, read);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
					}
					if (in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			return newsPath + fileName;
		}
		
		return "";
	}
}
